import { SET_PRODUCTS, EDIT_PRODUCTS, DEL_PRODUCTS } from './actionsType'
const initState = {
	products: []
}

const reducer = (state = initState, action) => {
	const { type, payload } = action
	const { products } = state
	switch (type) {
		case SET_PRODUCTS:
			return {
				...state,
				products: payload
			};
		case EDIT_PRODUCTS:
			const item = products.find(v => v.id === payload.id)
			Object.assign(item, payload)
			return {
				...state,
				products: [...products]
			}
		case DEL_PRODUCTS:
			const newProducts = products.filter(v => v.id !== payload.id)
			return {
				...state,
				products: [...newProducts]
			}
		default:
			return state
	}
}


export default reducer